package com.zzy.threadtest;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ZzyThreadPoolTaskExecutor {


   public void test(int threadCount){
//       ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
//       threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);//等待线程执行完 关闭 线程池
//       threadPoolTaskExecutor.setAllowCoreThreadTimeOut(true);//核心线程是否允许收回关闭
//       // 设置线程池中任务的等待时间，如果超过这个时候还没有销毁就强制销毁，以确保能够被关闭，而不是阻塞住
//       threadPoolTaskExecutor.setAwaitTerminationSeconds(30);
//       threadPoolTaskExecutor.setCorePoolSize(10);//核心线程数量
//       threadPoolTaskExecutor.setMaxPoolSize(100);//最大线程数
//       //当任务处理完以后，线程池中的线程数还大于corePoolSize，那么多余的 线程会被回收。也就是超时回收时间
//       threadPoolTaskExecutor.setKeepAliveSeconds(1000);
//       // 线程池名的前缀
//       threadPoolTaskExecutor.setThreadNamePrefix("zzy-test-");
//       //当线程大于corePoolSize个的时候，将线程放入queueCapacity大小的队列
//       threadPoolTaskExecutor.setQueueCapacity(20);
//       //线程池的拒绝策略
//       threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());



       ExecutorService executor = Executors.newFixedThreadPool(10);


       List<Object> list = new ArrayList<>();
       List<CompletableFuture<String[]>> completableFutures = new ArrayList<>();

       for(int i=1;i<=threadCount;i++){
           int index = i;
           CompletableFuture<String[]> completableFuture = CompletableFuture
                   .supplyAsync(
                           ()->doSomeThing(index),executor
                   )
                   .thenApply( arrays->{
                               System.out.println("thenApply--->"+ Arrays.toString(arrays) );
                               return arrays;
                           }
                   )
                   .whenComplete( (resArray,ex) ->{
                               System.out.println("任务：" + resArray[0] + "完成。res--->" + resArray[1]  );
                               if(ex!=null){
                                   System.out.println("异常信息：" + ex.getMessage());
                               }else{
                                   System.out.println("无异常");
                               }

                               list.add(Arrays.toString(resArray));
                           }
                   );

           completableFutures.add(completableFuture);
       }

       CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()])).join();
       System.out.println("完成结果顺序：list=" + list);


       //executor.shutdown();//关闭线程池


   }

    public String[] doSomeThing(int i){
        String[] res = {""+i,"msg--"+i};
       return res;
    }


//    public static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<List<T>>> futureList){
//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()]));
//
//        return allOf.thenApply(strArr->{
//            return futureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
//        });
//    }
//
//    public static <T> CompletableFuture<List<T>> sequence(Stream<CompletableFuture<T>> futureList){
//       List< CompletableFuture<T>> futures = futureList.filter(f->f!=null).collect(Collectors.toList());
//       return sequence(futures);
//    }


    public static void main(String[] args) {
        ZzyThreadPoolTaskExecutor z = new ZzyThreadPoolTaskExecutor();
        z.test(5);
    }
}
